// 121. 买卖股票的最佳时机

function maxProfit(prices: number[]): number {
  let min = Number.MAX_SAFE_INTEGER,
    max = 0;

  for (const num of prices) {
    if (num < min) {
      // 记录下当前最小值
      min = num;
    } else if (num - min > max) {
      // 记录下差值的最大值
      max = num - min;
    }
  }

  return max;
}

const nums30 = [7, 1, 5, 3, 6, 4];
console.log(maxProfit(nums30));
